 // pages/suggestion/suggestion.js
 var util = require("../../utils/util.js");
Page({
    //分享
    onShareAppMessage: function () { },
    /**
     * 页面的初始数据
     */
    data: {
        imgHeight:0,
        imageArr:[0],
        image:{},
        defaultImg:"../../images/add.png",
        updatePath:[]
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var system = wx.getSystemInfoSync();
        var imgHeight = system.screenWidth;
        this.setData({
            imgHeight: imgHeight*0.18133
        });
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },
    //选择图片
    selectPic:function(options){
        var type = options.currentTarget.dataset.type;
        var index = options.currentTarget.dataset.index; 
        var imageData = this.data.image;
        var imageArr = this.data.imageArr;
        var that = this;
        var flag = !imageData[type];
        wx.chooseImage({
            count: 1,
            sizeType: ['original', 'compressed'],
            sourceType: ['album', 'camera'],
            success(res) {
                if (res.errMsg == "chooseImage:ok"){
                    // tempFilePath可以作为img标签的src属性显示图片
                    const tempFilePaths = res.tempFilePaths;
                    imageData[type] = tempFilePaths[0];
                    if(flag){
                        imageArr.push(index*1+1);
                    }
                    that.setData({
                        image: imageData,
                        imageArr: imageArr
                    });
                }
            }
        })
    },
    //输入框值设置
    setValue:function(options){
        var type = options.currentTarget.dataset.type;
        var value = options.detail.value;
        this.setData({
            [type]: value
        });
    },
    //提交数据
    submit:function(){
        var fanhui = this.data.fanhui;
        var phone = this.data.phone;
        if (!fanhui || fanhui ==""){
            wx.showToast({
                title: '请输入反馈！',
                icon:"none"
            });
            return false;
        }
        if (!phone || phone == "") {
            wx.showToast({
                title: '请输入手机号！',
                icon: "none"
            });
            return false;
        }
        var image = this.data.image;
        var temp = [];
        for(var key in image){
            temp.push(image[key]);
        }
        this.uploadImg(temp,{},0);
    },
    //上传图片
    uploadImg: function (data, jsonData, i) {
        var HEADER = util.HEADER;
        var that = this,
            success = data.success ? data.success : 0,//上传成功的个数
            fail = data.fail ? data.fail : 0;//上传失败的个数
        if (data.length < 1) {
            that.submitSuggest();
            return false;
        }
        wx.uploadFile({
            url: HEADER + 'admin/uploadImg', //仅为示例，非真实的接口地址
            filePath: data[i],
            name: 'file',
            formData: {},
            header: { "Content-Type": "multipart/form-data" },
            success: function (res) {
                var data = JSON.parse(res.data);
                if (data.result == 200 || data.result == "200") {
                    var oldArr = that.data.updatePath;
                    oldArr.push(data.data);
                    that.setData({
                        updatePath: oldArr
                    });
                }
            },
            fail: function () {
                fail++;//图片上传失败，图片上传失败的变量+1
            },
            complete: function () {
                i++;//这个图片执行完上传后，开始上传下一张
                if (i == data.length) {   //当图片传完时，停止调用   
                    that.submitSuggest();
                } else {//若图片还没有传完，则继续调用函数
                    data.i = i;
                    data.success = success;
                    data.fail = fail;
                    that.uploadImg(data, jsonData, i);
                }
            }
        });
    },
    //提交意见信息
    submitSuggest:function(){
        var that = this;
        var content = that.data.fanhui;
        var phone = that.data.phone;
        var uploadPath = that.data.updatePath;
        var userInfo = JSON.parse(wx.getStorageSync("userInfo"));
        var jsonData = {};
        if (!!uploadPath && uploadPath != "") {
            jsonData.img = uploadPath.join("|");
        }
        jsonData.adminid = userInfo.smAdmin.id;
        jsonData.content = content;
        jsonData.phone = phone;
        util.wxAjax("guestbook/save", "POST", jsonData, function (res) {
            var data = res.data;
            if (data.result == 200 || data.result == "200") {
                that.setData({
                    title: data.msg,
                    imageArr: [0],
                    image: {},
                    phone: "",
                    fanhui: ""
                });
                wx.showModal({
                    title: '提示信息',
                    content: '反馈成功，我们将会及时处理！',
                    success: function (res) {
                        if (res.confirm) {
                            wx.navigateBack();
                        }
                    }
                })
            } else {
                wx.showToast({
                    title: data.msg,
                    icon: "none"
                })
            }
        });
    }
})